package demo1;

public class MyLinkList implements IList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }

    public ListNode head;
    public ListNode last;

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public boolean contain(int num) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == num) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void clear() {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            ListNode curn = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curn;
        }
        head = last = null;
    }

    @Override
    public void addFirst(int num) {
        ListNode node = new ListNode(num);
        if (head == null) {
            head = last = node;
        } else {
            node.next = head;//先绑后继
            head.prev = node;//邦前驱
            head = node;//移动头指针
        }
    }

    @Override
    public void addLast(int num) {
        ListNode node=new ListNode(num);
        if(head==null){
            head=last=node;
        }else{
            last.next=node;//改后继
            node.prev=last;//改前驱
            last=last.next;//移动尾节点标记
        }
    }

    @Override
    public void addIndex(int num, int index) {
        if(head==null){
            return;
        }
        if(index<0||index>size()){
            throw new RuntimeException("下标不合法！！");
        }
        if(index==0){
            addFirst(num);
            return;
        }
        if(index==size()){
            addLast(num);
        }
        ListNode node=new ListNode(num);
        ListNode cur=findIndex(index);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
    }
    private ListNode findIndex(int index){
        ListNode cur=head;
        while (index!=0){
            index--;
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public void remove(int key) {
        if(head==null){
            System.out.println("链表为空！！");
            return;
        }
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;//删除头结点
                    if(head!=null){
                        head.prev=null;//防止只有一个节点
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur.next==null){
                        last=last.prev;//删除尾节点
                    }else{
                        cur.next.prev=cur.prev;//删除中间
                    }

                }
                return;
            }else{
                cur=cur.next;
            }
        }
    }

    @Override
    public void removeAllkeys(int key) {
        if(head==null){
            System.out.println("链表为空！！！");
        }
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }
                else{
                    cur.prev.next=cur.next;
                    if(cur.next==null){
                        last=last.prev;
                    }else{
                        cur.next.prev=cur.prev;
                    }
                }
            }
                cur=cur.next;//可处理多个连续重复与不连续重复

        }
    }


}